#!/bin/bash
echo "应用 AICoder 补丁到 cbp2make..."

cd /d/vc/UEStudio/src/tools/cbp2make_ai

# 备份原始文件
cp src/cbproject.cpp src/cbproject.cpp.original
cp src/cbp2make.cpp src/cbp2make.cpp.original

# 在 cbproject.cpp 中添加 AI 路径修正
sed -i '/#include "cbproject.h"/a #include "aicoder_enhancements.cpp"' src/cbproject.cpp

# 在路径处理函数附近添加 AI 修正
sed -i '/GetCompilerOptions\|GetLinkerOptions/ {
    a \
    // AICoder 路径修正\
    for (int i = 0; i < m_CompilerDirectories.GetCount(); i++) {\
        m_CompilerDirectories[i] = AICoder_FixPath(m_CompilerDirectories[i].GetCString());\
    }\
    for (int i = 0; i < m_LinkerDirectories.GetCount(); i++) {\
        m_LinkerDirectories[i] = AICoder_FixPath(m_LinkerDirectories[i].GetCString());\
    }
}' src/cbproject.cpp

# 在 cbp2make.cpp 中添加 AI 模式支持
sed -i '/main(/ {
    a \
    // AICoder 增强模式\
    if (argc > 1 && std::string(argv[1]) == "--ai-mode") {\
        AICoder_AnalyzeProject(argv[2]);\
        return 0;\
    }
}' src/cbp2make.cpp

echo "AICoder 补丁应用完成!"
echo "编译命令: g++ -o bin/cbp2make_ai src/*.cpp -Ilib -Ilib/tinyxml -Isrc -Wall -std=c++11"
